package com.sskj.one.transaction;

import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.bulong.rudeness.RudenessScreenHelper;
import com.github.tifezh.kchartlib.chart.BaseKChartView;
import com.github.tifezh.kchartlib.chart.KChartView;
import com.github.tifezh.kchartlib.chart.base.IDateTimeFormatter;
import com.github.tifezh.kchartlib.chart.formatter.TimeFormatter;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.NumberUtil;
import com.sskj.common.util.TimeFormatUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.ColorEnum;
import com.sskj.one.R;
import com.sskj.one.bean.BuyOrderBean;
import com.sskj.one.bean.Stock;
import com.sskj.one.bean.bus.BuyBean;
import com.sskj.one.util.ArithUtil;
import com.sskj.one.util.DataUtil;
import com.sskj.one.view.KChartAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * @author Hey
 * Create at  2019/05/28
 */
public class ChartFragment extends BaseFragment<ChartPresenter> {
    @BindView(R.id.tv_now_price)
    TextView tvNowPrice;
    @BindView(R.id.tv_change_price)
    TextView tvChangePrice;
    @BindView(R.id.tv_change_rate)
    TextView tvChangeRate;
    @BindView(R.id.tv_highest_price)
    TextView tvHighestPrice;
    @BindView(R.id.tv_open_price)
    TextView tvOpenPrice;
    @BindView(R.id.tv_lowest_price)
    TextView tvLowestPrice;
    @BindView(R.id.tv_yesterday_income)
    TextView tvYesterdayIncome;
    @BindView(R.id.chartView)
    KChartView chartView;
    private KChartAdapter mAdapter;
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private float price;
    private Paint buySellPricePaint;

    private String buytype;

    private String nowPrice = "";//当前价
    private String buySellPrice = "";//下单价
    private Double afterUnwindNowPrice;//平仓后3秒当前值
    private Double afterUnwindPrice;//平仓后3秒值
    private Double fengkongPrice1;//风控价1
    private Double fengkongPrice2;//风控价2
    private Double fengkongPrice3;//风控价2
    private Double fengkongPrice4;//风控价2
    private Double fengkongPrice5;//风控价2
    private Paint buySellLinePaint;

    private boolean isFengkong;
    private DisposableSubscriber<Long> disposableSubscriber;

    @Override
    public int getLayoutId() {
        return R.layout.app_fragment_chart;
    }

    @Override
    public ChartPresenter getPresenter() {
        return new ChartPresenter();
    }

    @Override
    public void initView() {
        RxBus.getDefault().register(this);
        initKChart();
    }

    @Override
    public void initData() {
        mPresenter.getStockInfo();
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void upDate(CoinBean data) {
        if (data.getCode().equals("USDCNY") && mAdapter.getCount() > 0) {
            nowPrice = data.getPrice();
            //增加买入价格虚横线（红色），实时线根据走势变换颜色，
            // 当实时价格在买入线上方，则实时线颜色为红色，当实时价格在买入线下方，则实时线颜色为绿色，自动变换！
            // （实时价格显示也跟随实时线变换颜色）
            if (isEmpty(buySellPrice) || isEmpty(buytype)) {
                buySellLinePaint.setColor(Color.WHITE);
            } else if (chartView != null && buySellLinePaint != null) {
                // 买入价大于实时价
                // 买涨：亏（绿色）
                // 买跌：盈（红色）
                if (Float.valueOf(buySellPrice) > Float.valueOf(data.getPrice())) {
                    if (buytype.equals(Constans.BUY_UP)) {
                        buySellLinePaint.setColor(ContextCompat.getColor(App.INSTANCE, R.color.theme));
                    } else if (buytype.equals(Constans.BUY_DOWN)) {
                        buySellLinePaint.setColor(ContextCompat.getColor(App.INSTANCE, R.color.color_red));
                    }
                    // 买入价小于实时价
                    // 买涨：盈（红色）
                    // 买跌：亏（绿色）
                } else {
                    if (buytype.equals(Constans.BUY_UP)) {
                        buySellLinePaint.setColor(ContextCompat.getColor(App.INSTANCE, R.color.color_red));
                    } else if (buytype.equals(Constans.BUY_DOWN)) {
                        buySellLinePaint.setColor(ContextCompat.getColor(App.INSTANCE, R.color.theme));
                    }
                }
            }
            tvNowPrice.setText(NumberUtil.keep(data.getPrice(), 5));
            tvChangePrice.setText(data.getChange().contains("-") ? NumberUtil.keep(data.getChange(), 5) : "+" + NumberUtil.keep(data.getChange(), 5));
            tvChangeRate.setText(data.getChangeRate().contains("-") ? data.getChangeRate() : "+" + data.getChangeRate());
            tvHighestPrice.setText(NumberUtil.keep(data.getHigh(), 5));
            tvLowestPrice.setText(NumberUtil.keep(data.getLow(), 5));
            tvOpenPrice.setText(NumberUtil.keep(data.getOpen(), 5));
            tvYesterdayIncome.setText(NumberUtil.keep(data.getPrevClose(), 5));
            tvNowPrice.setTextColor(ContextCompat.getColor(App.INSTANCE,
                    data.isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()));
            tvChangePrice.setTextColor(ContextCompat.getColor(App.INSTANCE,
                    data.isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()));
            tvChangeRate.setTextColor(ContextCompat.getColor(App.INSTANCE,
                    data.isUp() ? ColorEnum.UP.getColor() : ColorEnum.DOWN.getColor()));
            Stock lastItem = (Stock) mAdapter.getItem(mAdapter.getCount() - 1);
            price = lastItem.getPrice();

            Stock stock = copyStock(data);
            List<Stock> stocks = new ArrayList<>();
            if (Double.valueOf(data.getPrice()) > lastItem.getHigh()) {
                lastItem.setHigh(Double.valueOf(data.getPrice()));
            }
            if (Double.valueOf(data.getPrice()) < lastItem.getLow()) {
                lastItem.setLow(Double.valueOf(data.getPrice()));
            }
            if (!isFengkong) {
                stock.setOpen(Double.valueOf(data.getPrice()));
                stocks.add(stock);
                DataUtil.calculate(stocks);
                mAdapter.addNewData(stocks);
            }

//            if (isAdd(timeStamp, Long.valueOf(TimeFormatUtil.parseLongE(lastItem.getTimestamp())) * 1000)) {
//                Stock stock = copyStock(data);
//                List<Stock> stocks = new ArrayList<>();
//                stock.setOpen(lastItem.getClosePrice());
//                stock.setHigh(lastItem.getPrice());
//                stock.setLow(lastItem.getPrice());
//                stock.setVolume(lastItem.getVolume());
//                stocks.add(stock);
//                DataUtil.calculate(stocks);
//                mAdapter.addNewData(stocks);
////                    mPresenter.getStockInfo();
//            } else {
//                if (Double.valueOf(data.getPrice()) > lastItem.getHigh()) {
//                    lastItem.setHigh(Double.valueOf(data.getPrice()));
//                }
//                if (Double.valueOf(data.getPrice()) < lastItem.getLow()) {
//                    lastItem.setLow(Double.valueOf(data.getPrice()));
//                }
//                lastItem.setClose(Double.valueOf(data.getPrice()));
//                mAdapter.getDatas().set(mAdapter.getCount() - 1, lastItem);
//                mAdapter.changeLastItemClosePrice(Float.valueOf(data.getPrice()));
//            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN, code = RxBusCode.REFRESH_HANGQING)
    public void refresh() {
        if (mPresenter != null && !isFengkong) {
            mPresenter.getStockInfo();
        }
    }
//
//    @Override
//    public void onResume() {
//        super.onResume();
//        if (mPresenter != null && !isFengkong) {
//            mPresenter.getStockInfo();
//        }
//    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void drawBuy(BuyOrderBean buyOrderBean) {
        if (chartView == null) {
            return;
        }
        if (!isEmpty(buyOrderBean.getBuyprice())) {
            isFengkong = false;
            if (disposableSubscriber != null && !disposableSubscriber.isDisposed()) {
                disposableSubscriber.dispose();
                disposableSubscriber = null;
            }
            buytype = buyOrderBean.getType();
            chartView.setDrawBuySellLine(true);
            buySellPrice = buyOrderBean.getBuyprice();
            chartView.setBuySellPrice(Float.valueOf(buyOrderBean.getBuyprice()));
            disposableSubscriber = new DisposableSubscriber<Long>() {
                @Override
                public void onNext(Long aLong) {
                    Log.i("aaaaaaaaaaaaaaaaaaaa", "onNext: " + (Long.valueOf(buyOrderBean.getCycle_time()) + 4 - aLong));
                    //平仓倒计时5秒
                    //如果当前单被风控
                    //根据第15s当前价以及最终风控平仓价的差值，在最终5秒以0.6:0.4:0.8:0.6比例上升或均值下降
                    if ("1".equals(buyOrderBean.getFengkong_start()) && !isEmpty(nowPrice) && Long.valueOf(buyOrderBean.getCycle_time()) + 4 - aLong == 9) {
                        fengkongPrice5 = Double.valueOf(NumberUtil.keep(buyOrderBean.getSellprice3(), 5));
                        if (Double.valueOf(nowPrice) > fengkongPrice5) {
                            double sub = ArithUtil.sub(Double.valueOf(nowPrice), fengkongPrice5);
                            fengkongPrice1 = ArithUtil.sub(Double.valueOf(nowPrice), ArithUtil.mul(sub, 0.6));
                            fengkongPrice2 = ArithUtil.sub(Double.valueOf(nowPrice), ArithUtil.mul(sub, 0.4));
                            fengkongPrice3 = ArithUtil.sub(Double.valueOf(nowPrice), ArithUtil.mul(sub, 0.8));
                            fengkongPrice4 = ArithUtil.sub(Double.valueOf(nowPrice), ArithUtil.mul(sub, 0.6));
                        } else if (Double.valueOf(nowPrice) < fengkongPrice5) {
                            double sub = ArithUtil.sub(fengkongPrice5, Double.valueOf(nowPrice));
                            fengkongPrice1 = ArithUtil.sub(fengkongPrice5, ArithUtil.mul(sub, 0.6));
                            fengkongPrice2 = ArithUtil.sub(fengkongPrice5, ArithUtil.mul(sub, 0.8));
                            fengkongPrice3 = ArithUtil.sub(fengkongPrice5, ArithUtil.mul(sub, 0.4));
                            fengkongPrice4 = ArithUtil.sub(fengkongPrice5, ArithUtil.mul(sub, 0.6));
                        } else if (Double.valueOf(nowPrice).equals(fengkongPrice5)) {
                            fengkongPrice1 = fengkongPrice5;
                            fengkongPrice2 = fengkongPrice5;
                            fengkongPrice3 = fengkongPrice5;
                            fengkongPrice4 = fengkongPrice5;
                        }
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "nowPrice: " + nowPrice);
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "fengkongPrice5: " + fengkongPrice5);
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "----------------------------------------------------------------------------------------");
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "fengkongPrice1: " + fengkongPrice1);
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "fengkongPrice2: " + fengkongPrice2);
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "fengkongPrice3: " + fengkongPrice3);
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "fengkongPrice4: " + fengkongPrice4);
                    }
                    if ("1".equals(buyOrderBean.getFengkong_start()) && !isEmpty(nowPrice)) {
                        if (Long.valueOf(buyOrderBean.getCycle_time()) + 4 - aLong < 9 && mAdapter.getCount() > 0) {
                            isFengkong = true;
                        }
                        //平仓倒计时4秒
                        if (Long.valueOf(buyOrderBean.getCycle_time()) + 4 - aLong == 8 && mAdapter.getCount() > 0) {
                            Stock lastStock = mAdapter.getDatas().get(mAdapter.getDatas().size() - 1);
                            Long newTime = Long.valueOf(TimeFormatUtil.parseLongE(lastStock.getTimestamp())) * 1000 + 1000;
                            Stock stock = new Stock();
                            List<Stock> stocks = new ArrayList<>();
                            stock.setOpen(fengkongPrice1);
                            stock.setTimestamp(TimeFormatUtil.formatE(newTime));
                            stocks.add(stock);
                            mAdapter.addNewData(stocks);
                        }
                        //平仓倒计时3秒
                        if (Long.valueOf(buyOrderBean.getCycle_time()) + 4 - aLong == 7 && mAdapter.getCount() > 0) {
                            Stock lastStock = mAdapter.getDatas().get(mAdapter.getDatas().size() - 1);
                            Long newTime = Long.valueOf(TimeFormatUtil.parseLongE(lastStock.getTimestamp())) * 1000 + 1000;
                            Stock stock = new Stock();
                            List<Stock> stocks = new ArrayList<>();
                            stock.setOpen(fengkongPrice2);
                            stock.setTimestamp(TimeFormatUtil.formatE(newTime));
                            stocks.add(stock);
                            mAdapter.addNewData(stocks);
                        }
                        //平仓倒计时2秒
                        if (Long.valueOf(buyOrderBean.getCycle_time()) + 4 - aLong == 6 && mAdapter.getCount() > 0) {
                            Stock lastStock = mAdapter.getDatas().get(mAdapter.getDatas().size() - 1);
                            Long newTime = Long.valueOf(TimeFormatUtil.parseLongE(lastStock.getTimestamp())) * 1000 + 1000;
                            Stock stock = new Stock();
                            List<Stock> stocks = new ArrayList<>();
                            stock.setOpen(fengkongPrice3);
                            stock.setTimestamp(TimeFormatUtil.formatE(newTime));
                            stocks.add(stock);
                            mAdapter.addNewData(stocks);
                        }
                        //平仓倒计时1秒
                        if (Long.valueOf(buyOrderBean.getCycle_time()) + 4 - aLong == 5 && mAdapter.getCount() > 0) {
                            Stock lastStock = mAdapter.getDatas().get(mAdapter.getDatas().size() - 1);
                            Long newTime = Long.valueOf(TimeFormatUtil.parseLongE(lastStock.getTimestamp())) * 1000 + 1000;
                            Stock stock = new Stock();
                            List<Stock> stocks = new ArrayList<>();
                            stock.setOpen(fengkongPrice4);
                            stock.setTimestamp(TimeFormatUtil.formatE(newTime));
                            stocks.add(stock);
                            DataUtil.calculate(stocks);
                            mAdapter.addNewData(stocks);
                        }
                        //平仓倒计时0秒
                        if (Long.valueOf(buyOrderBean.getCycle_time()) + 4 - aLong == 4 && mAdapter.getCount() > 0) {
                            Stock lastStock = mAdapter.getDatas().get(mAdapter.getDatas().size() - 1);
                            Long newTime = Long.valueOf(TimeFormatUtil.parseLongE(lastStock.getTimestamp())) * 1000 + 1000;
                            Stock stock = new Stock();
                            List<Stock> stocks = new ArrayList<>();
                            stock.setOpen(fengkongPrice5);
                            stock.setTimestamp(TimeFormatUtil.formatE(newTime));
                            stocks.add(stock);
                            DataUtil.calculate(stocks);
                            mAdapter.addNewData(stocks);
                        }
                    }
                    //平仓后第1秒
                    if (Long.valueOf(buyOrderBean.getCycle_time()) + 4 - aLong == 3 && mAdapter.getCount() > 0) {
                        //移除下单价线
                        buySellPrice = "";
                        chartView.setDrawBuySellLine(false);

                        afterUnwindPrice = Double.valueOf(NumberUtil.keep(buyOrderBean.getSellprice3(), 5));
                        afterUnwindNowPrice = Double.valueOf(nowPrice);
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "----------------------------------------------------------------------------------------");
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "afterUnwindPrice3: " + afterUnwindPrice);
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "nowPrice3: " + afterUnwindNowPrice);
                        if (afterUnwindNowPrice > afterUnwindPrice) {
                            double sub = ArithUtil.sub(afterUnwindNowPrice, afterUnwindPrice);
                            try {
                                afterUnwindPrice = ArithUtil.add(afterUnwindPrice, ArithUtil.div(sub, 2, 6));
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        } else if (afterUnwindNowPrice < afterUnwindPrice) {
                            double sub = ArithUtil.sub(afterUnwindPrice, afterUnwindNowPrice);
                            try {
                                afterUnwindPrice = ArithUtil.add(afterUnwindNowPrice, ArithUtil.div(sub, 2, 6));
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "afterUnwindPrice3--: " + afterUnwindPrice);
                        Stock lastStock = mAdapter.getDatas().get(mAdapter.getDatas().size() - 1);
                        Long newTime = Long.valueOf(TimeFormatUtil.parseLongE(lastStock.getTimestamp())) * 1000 + 1000;
                        Stock stock = new Stock();
                        List<Stock> stocks = new ArrayList<>();
                        stock.setOpen(afterUnwindPrice);
                        stock.setTimestamp(TimeFormatUtil.formatE(newTime));
                        stocks.add(stock);
                        DataUtil.calculate(stocks);
                        mAdapter.addNewData(stocks);
                    }
                    //平仓后第2秒
                    if (Long.valueOf(buyOrderBean.getCycle_time()) + 4 - aLong == 2 && mAdapter.getCount() > 0) {
                        afterUnwindNowPrice = Double.valueOf(nowPrice);
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "----------------------------------------------------------------------------------------");
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "afterUnwindPrice2: " + afterUnwindPrice);
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "nowPrice2: " + afterUnwindNowPrice);
                        if (Double.valueOf(nowPrice) > afterUnwindPrice) {
                            double sub = ArithUtil.sub(afterUnwindNowPrice, afterUnwindPrice);
                            try {
                                afterUnwindPrice = ArithUtil.add(afterUnwindPrice, ArithUtil.div(sub, 2, 6));
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        } else if (afterUnwindNowPrice < afterUnwindPrice) {
                            double sub = ArithUtil.sub(afterUnwindPrice, afterUnwindNowPrice);
                            try {
                                afterUnwindPrice = ArithUtil.add(afterUnwindNowPrice, ArithUtil.div(sub, 2, 6));
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "afterUnwindPrice2--: " + afterUnwindPrice);
                        Stock lastStock = mAdapter.getDatas().get(mAdapter.getDatas().size() - 1);
                        Long newTime = Long.valueOf(TimeFormatUtil.parseLongE(lastStock.getTimestamp())) * 1000 + 1000;
                        Stock stock = new Stock();
                        List<Stock> stocks = new ArrayList<>();
                        stock.setOpen(afterUnwindPrice);
                        stock.setTimestamp(TimeFormatUtil.formatE(newTime));
                        stocks.add(stock);
                        DataUtil.calculate(stocks);
                        mAdapter.addNewData(stocks);
                    }
                    //平仓后第3秒
                    if (Long.valueOf(buyOrderBean.getCycle_time()) + 4 - aLong == 1 && mAdapter.getCount() > 0) {
                        afterUnwindNowPrice = Double.valueOf(nowPrice);
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "----------------------------------------------------------------------------------------");
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "afterUnwindPrice1: " + afterUnwindPrice);
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "nowPrice1: " + afterUnwindNowPrice);
                        if (afterUnwindNowPrice > afterUnwindPrice) {
                            double sub = ArithUtil.sub(afterUnwindNowPrice, afterUnwindPrice);
                            try {
                                afterUnwindPrice = ArithUtil.add(afterUnwindPrice, ArithUtil.div(sub, 2, 6));
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        } else if (afterUnwindNowPrice < afterUnwindPrice) {
                            double sub = ArithUtil.sub(afterUnwindPrice, afterUnwindNowPrice);
                            try {
                                afterUnwindPrice = ArithUtil.add(afterUnwindNowPrice, ArithUtil.div(sub, 2, 6));
                            } catch (IllegalAccessException e) {
                                e.printStackTrace();
                            }
                        }
                        Log.i("aaaaaaaaaaaaaaaaaaaa", "afterUnwindPrice1--: " + afterUnwindPrice);
                        Stock lastStock = mAdapter.getDatas().get(mAdapter.getDatas().size() - 1);
                        Long newTime = Long.valueOf(TimeFormatUtil.parseLongE(lastStock.getTimestamp())) * 1000 + 1000;
                        Stock stock = new Stock();
                        List<Stock> stocks = new ArrayList<>();
                        stock.setOpen(afterUnwindPrice);
                        stock.setTimestamp(TimeFormatUtil.formatE(newTime));
                        stocks.add(stock);
                        DataUtil.calculate(stocks);
                        mAdapter.addNewData(stocks);
                    }
                }

                @Override
                public void onError(Throwable t) {
                }

                @Override
                public void onComplete() {
                    isFengkong = false;
                }
            };
            Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                    .take(Long.valueOf(buyOrderBean.getCycle_time()) + 4)
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(disposableSubscriber);
        } else {
            buySellPrice = "";
            chartView.setDrawBuySellLine(false);
        }
    }

    private void initKChart() {
        chartView.setAlwaysDraw(true);
        chartView.setDateTimeFormatter(date -> TimeFormatUtil.SF_FORMAT_N.format(date));
        chartView.setBackgroundColor(ContextCompat.getColor(App.INSTANCE, R.color.inside));
        chartView.showLoading();
        chartView.setDrawMinuteStyle(true);
        chartView.setDrawTabView(true);
        chartView.setMinuteLineWidth((int) RudenessScreenHelper.dp2px(App.INSTANCE, 2));
        chartView.setLineWidth(RudenessScreenHelper.dp2px(App.INSTANCE, 4));
        chartView.setMainUpColor(R.color.theme);
        chartView.setMainDownColor(R.color.color_red);
        chartView.setGridRows(6);
        chartView.setLabelGravity(BaseKChartView.Gravity.LEFT);
        chartView.setPointWidth(RudenessScreenHelper.dp2px(App.INSTANCE, 2.5f));
        chartView.setDrawVolume(false);
        chartView.setShader(Color.parseColor("#9E02C0A6"), Color.parseColor("#4E02C0A6"), Color.parseColor("#0002C0A6"), 1600);
        chartView.getmCurrentPricePaint().setTextSize(30);
        chartView.setGridColumns(6);
        chartView.setVolumeMaGone(true);
        chartView.setDrawDown(false);
        chartView.getmCurrentPricePaint().setTextSize(RudenessScreenHelper.dp2px(App.INSTANCE, 12));
        chartView.getmCurrentPricePaint().setColor(ContextCompat.getColor(App.INSTANCE, R.color.color_red));
        chartView.setValueFormatter(v -> NumberUtil.keep(v, 5));
        mAdapter = new KChartAdapter();
        chartView.setAdapter(mAdapter);
        chartView.getBuySellPricePaint().setTextSize(RudenessScreenHelper.dp2px(App.INSTANCE, 10));
        buySellLinePaint = chartView.getBuySellLinePaint();

    }

    public void setChart(List<Stock> stockList) {
        if (chartView != null) {
            chartView.hideLoading();
            if (stockList == null)
                return;
            Collections.reverse(stockList);
            DataUtil.calculate(stockList);
            if (price != 0 && stockList.size() > 0) {
                stockList.get(stockList.size() - 1).setOpen(price);
            }
            mAdapter.updataData(stockList);
        }
    }


    public Stock copyStock(CoinBean newStock) {
        Stock stock = new Stock();
        try {
            long timeStamp = dateFormat.parse(newStock.getDate() + " " + newStock.getTime()).getTime();
            stock.setCode(newStock.getCode());
            stock.setClosePrice(Float.valueOf(newStock.getClose()));
            stock.setTimestamp(TimeFormatUtil.formatE(timeStamp));
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return stock;
    }

    private boolean isAdd(long newTime, long lastTime) {
        boolean add = false;
        newTime = newTime / 1000;
        lastTime = lastTime / 1000;
        long time = newTime - lastTime;
        add = time >= 1 * 60;
        return add;
    }

    public static ChartFragment newInstance() {
        ChartFragment fragment = new ChartFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (disposableSubscriber != null && !disposableSubscriber.isDisposed()) {
            disposableSubscriber.dispose();
            disposableSubscriber = null;
        }
    }
}
